package torres.view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.util.HashMap;

import javax.swing.JPanel;

import torres.controller.Direction;
import torres.entity.Entity;
import torres.map.World;

@SuppressWarnings("serial")
public class ActionMenuView extends JPanel {
	
	int screenWidth ,x, width;
	int screenHeight, y, height;
	Entity player;
	HashMap<Integer, Dimension> actionMenuTable;
	
	public ActionMenuView() {
		
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		screenWidth = screenSize.width;
		screenHeight = screenSize.height;
		
		x = screenWidth/9;
		y = 4*screenHeight/13;
		
		setIgnoreRepaint(true);
		setPreferredSize(new Dimension(x,y));
		setLocation(screenWidth-x/2, screenHeight-y/2);
		setLayout(null);
		setVisible(true);
		setFocusable(true);
		setOpaque(true);
		
		player = World.getInstance().getPlayer();
	}
	
	public void paintComponent(Graphics g) {
		
		super.paintComponent(g);
		Graphics2D g2 = (Graphics2D) g;
		
		g.setColor(Color.black);
		drawWindow(g);
	
//		int yOffset = 30;
//		int xOffset = 20;
		
		Font body = new Font("Helvetica",Font.BOLD, 22);
		
		g.setFont(body);
		g.setColor(Color.black);
		drawActions(g2);
	}
	
	public void drawWindow(Graphics g) {
		player = World.getInstance().getPlayer();
		ImageLoader loader = new ImageLoader("actionMenuTexture.jpg");
		BufferedImage img = loader.getImage();
		Direction dir = player.getDirection();
		switch(dir) {
			case NORTH: 	g.drawImage(img, screenWidth/2-x/2,5*screenHeight/7-y/2,x,y,null);
							break;
			case NORTHWEST: g.drawImage(img, screenWidth/2-x/2,5*screenHeight/7-y/2,x,y,null);
			  				break;
			case NORTHEAST: g.drawImage(img, screenWidth/2-x/2,5*screenHeight/7-y/2,x,y,null);
			  				break;
			case SOUTH: 	g.drawImage(img, screenWidth/2-x/2,2*screenHeight/7-y/2,x,y,null);
			  				break;
			case SOUTHWEST: g.drawImage(img, screenWidth/2-x/2,2*screenHeight/7-y/2,x,y,null);
			  				break;
			case SOUTHEAST: g.drawImage(img, screenWidth/2-x/2,2*screenHeight/7-y/2,x,y,null);
			  				break;
			case EAST: 		g.drawImage(img, 2*screenWidth/5-x/2,screenHeight/2-y/2,x,y,null);
			  				break;
			case WEST: 		g.drawImage(img, 3*screenWidth/5-x/2,screenHeight/2-y/2,x,y,null);
			  				break;
		}
	}
	
	public void drawActions(Graphics2D g2) {
		player = World.getInstance().getPlayer();
		actionMenuTable = new HashMap<Integer, Dimension>();
		Direction dir = player.getDirection();
		width = screenWidth/2;
		height = screenHeight/2;
		int xx = 1;
		int yy = 1;
		int yOffset = screenHeight/18;
		switch(dir) {
			case NORTH: 	xx = screenWidth/2-x/2;
							yy = 5*screenHeight/7-y/2;
							break;
			case NORTHWEST: xx = screenWidth/2-x/2;
							yy = 5*screenHeight/7-y/2;
			  				break;
			case NORTHEAST: xx = screenWidth/2-x/2;
							yy = 5*screenHeight/7-y/2;
			  				break;
			case SOUTH: 	xx = screenWidth/2-x/2;
							yy = 2*screenHeight/7-y/2;
			  				break;
			case SOUTHWEST: xx = screenWidth/2-x/2; 
							yy = 2*screenHeight/7-y/2;
			  				break;
			case SOUTHEAST: xx = screenWidth/2-x/2; 
							yy = 2*screenHeight/7-y/2;
			  				break;
			case EAST: 		xx = 2*screenWidth/5-x/2; 
							yy = screenHeight/2-y/2;
			  				break;
			case WEST: 		xx = 3*screenWidth/5-x/2; 
							yy = screenHeight/2-y/2;
			  				break;
		}
		width = width/5+width/47;
		g2.draw3DRect(xx,yy+yOffset/6, width, height/9, true);
		g2.draw3DRect(xx,yy+yOffset/6, width, 2*height/9, true);
		g2.draw3DRect(xx,yy+yOffset/6, width, 3*height/9, true);
		g2.draw3DRect(xx,yy+yOffset/6, width, 4*height/9, true);
		g2.draw3DRect(xx,yy+yOffset/6, width, 5*height/9, true);
		g2.drawString("Talk", xx+xx/100,yy+yOffset);
		g2.drawString("Attack", xx+xx/100,yy+2*yOffset);
		if(!World.getInstance().getPlayer().getOccupation().getJob().name().equals("SNEAK")) {
			g2.setFont(g2.getFont().deriveFont(Font.ITALIC|Font.BOLD));
			g2.setColor(Color.DARK_GRAY);
		}
		g2.drawString("Use Skill ->", xx+xx/100,yy+3*yOffset);
		g2.setFont(g2.getFont().deriveFont(Font.BOLD));
		g2.setColor(Color.black);
		g2.drawString("Use Item ->", xx+xx/100,yy+4*yOffset);
		if(!World.getInstance().getPlayer().getInteractedWith().getDisplayName().equals("merchant")) {
			g2.setFont(g2.getFont().deriveFont(Font.ITALIC|Font.BOLD));
			g2.setColor(Color.DARK_GRAY);
		}
		g2.drawString("Shop", xx+xx/100,yy+5*yOffset);
		for(int i = 1; i <= 5; ++i) {
			actionMenuTable.put(i, new Dimension(xx, yy+(i-1)*yOffset));
		}
		actionMenuTable.put(0, new Dimension(xx+screenWidth/8, yy+screenHeight/100));
		height = height/9;
	}
	
	public void update(Graphics g) {
		paintComponent(g);
	}
	
	public HashMap<Integer,Dimension> getActionMenuTable() {
		return actionMenuTable;
	}
	
	public int getWidth() {
		return width;
	}
	
	public int getHeight() {
		return height;
	}
}
